package com.jy.rhin.support.parsing;

import com.alibaba.fastjson.JSONArray;
import com.jy.rhin.application.enums.DocumentTypeEnum;
import com.jy.rhin.facade.dto.DocumentModule;
import com.jy.rhin.facade.dto.TemplateRespDto;
import com.jy.rhin.support.parsing.enums.ReportElementEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Description: 传染病报告
 * @Author: lww
 * @Date: 2022/6/21 9:14
 */
@Component
public class InfectiousDiseaseReportParser extends BaseDocumentParser {

    @Override
    public DocumentTypeEnum getDocumentType() {
        return DocumentTypeEnum.INFECTIOUS_DISEASE_REPORT;
    }

    /**
     * 传染病报告文档解析
     *
     * @param json             xmlJSON
     * @param templateRespList 查询返回的参数
     */
    @Override
    public List<TemplateRespDto> documentParsing(Object json, List<TemplateRespDto> templateRespList) {
        //文档头部分
        List<TemplateRespDto> headList = new ArrayList<>();
        LinkedHashMap<ReportElementEnum, List<TemplateRespDto>> reportListMap = new LinkedHashMap<>();
        String arrayType = null;
        String parentType = null;
        //补充对应文档章节字段List
        for (TemplateRespDto templateRespDto : templateRespList) {
            String element = templateRespDto.getYuanSuMC();
            String type = templateRespDto.getType();
            //list节点遍历完成时重新赋值
            if ((!StringUtils.isEmpty(arrayType)) && compareReportNum(element, arrayType, type)) {
                arrayType = null;
            }
            //文档体部分
            if (!StringUtils.isEmpty(type) || Objects.nonNull(ReportElementEnum.lookup(arrayType))) {
                ReportElementEnum[] values = ReportElementEnum.values();
                //根据不同章节部分添加字段list
                for (ReportElementEnum reportElementEnum : values) {
                    //章节类型和对应的枚举名称比对或者章节对应编码与枚举编码比对，存在父级枚举编码时需要比对父级枚举编码
                    if (Objects.equals(reportElementEnum.getName(), arrayType) || compareReportCode(type, parentType, reportElementEnum)) {
                        arrayType = reportElementEnum.getName();
                        //Component部分添加父级节点名称
                        if (Objects.equals(COMPONENT, reportElementEnum.getType()) && !StringUtils.isEmpty(type)) {
                            parentType = type;
                        }
                        //map-key存在时添加节点
                        if (reportListMap.containsKey(reportElementEnum)) {
                            List<TemplateRespDto> templateRespDtoListMap = reportListMap.get(reportElementEnum);
                            templateRespDtoListMap.add(templateRespDto);
                            //map-key不存在存在时初始化节点
                        } else {
                            List<TemplateRespDto> templateRespDtoListMap = new ArrayList<>();
                            templateRespDtoListMap.add(templateRespDto);
                            reportListMap.put(reportElementEnum, templateRespDtoListMap);
                        }
                        break;
                    }
                }
                //文档头部分
            } else {
                headList.add(templateRespDto);
            }
        }
        //获取value值
        //添加文档头部部分
        pathValue(headList, json, null);
        List<TemplateRespDto> templateRespDtoList = new ArrayList<>(headList);
        JSONArray componentArray = objectPathArray(COMPONENT_KEY, json);
        //循环遍历所有章节部分并获取对应值
        for (Map.Entry<ReportElementEnum, List<TemplateRespDto>> entry : reportListMap.entrySet()) {
            ReportElementEnum key = entry.getKey();
            List<TemplateRespDto> valueList = entry.getValue();
            if (Objects.equals(COMPONENT, key.getType())) {
                Object object = arrayPathObject(SECTION_CODE, key.getCode(), componentArray);
                pathValue(valueList, object, 3);
            } else {
                Object parentObject = arrayPathObject(SECTION_CODE, key.getParentCode(), componentArray);
                Object object = objectPathObject(SECTION_ENTRY, parentObject);
                if (object instanceof JSONArray) {
                    JSONArray entryArray = (JSONArray) object;
                    object = arrayPathObject(OBSERVATION_CODE, key.getCode(), entryArray);
                }
                pathValue(valueList, object, 5);
            }
            templateRespDtoList.addAll(valueList);
        }
        return templateRespDtoList;
    }


    /**
     * 父级节点不为空时比对父级code
     *
     * @param type
     * @param parentType
     * @param reportElement
     * @return
     */
    private boolean compareReportCode(String type, String parentType, ReportElementEnum reportElement) {
        String parentCode = reportElement.getParentCode();
        if (Objects.nonNull(parentCode)) {
            return (Objects.equals(type, reportElement.getCode()) && Objects.equals(parentType, parentCode));
        } else {
            return Objects.equals(type, reportElement.getCode());
        }
    }

    /**
     * 校验是否需要重新赋值arrayType
     *
     * @param element
     * @param arrayType
     * @param type
     * @return
     */
    private boolean compareReportNum(String element, String arrayType, String type) {
        return !StringUtils.isEmpty(type) || containsNumber(element) <= Integer.parseInt(arrayType.substring(arrayType.indexOf(SPOT) + 1));
    }


    @Override
    public List<DocumentModule> createBodyModule() {
        List<DocumentModule> list = new ArrayList<>();
        //文档体模块
        list.add(new DocumentModule("症状章节 ","/component/structuredBody/component/section/code[@code='11450-4']"));
        list.add(new DocumentModule("诊断记录章节","/component/structuredBody/component/section/code[@code='29548-5']"));
        list.add(new DocumentModule("死亡信息章节","/component/structuredBody/component/section/code[@displayName='传染病死亡信息']"));
        list.add(new DocumentModule("行政管理章节","/component/structuredBody/component/section/code[@displayName='行政管理']"));
        return list;
    }

}
